package de.revdev.llt.bean.utility;

import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

/**
 *
 * @author f.lindermaier
 */
@RequestScoped
public class MessageProvider {

    private FacesContext context;
    @Inject
    @MessageBundle
    private ResourceBundle bundle;

    public String getValue(String key) {
        String msg;
        try {
            msg = bundle.getString(key);
        } catch (MissingResourceException e) {
            msg = "??" + key + "?? is missing!";
        }
        return msg;
    }

    public void addMessage(String uiComponentId, FacesMessage.Severity severity, String key) {
        context.addMessage(uiComponentId, new FacesMessage(severity, getValue(key), getValue(key)));
    }

    @Inject
    public void setFacesContext(FacesContext facesContext) {
        this.context = facesContext;
    }

    public void setResourceBundle(ResourceBundle resourceBundle) {
        this.bundle = resourceBundle;
    }
}
